package cn.edu.henu.team.ui;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Toast;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobGeoPoint;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.adapter.FuUserAdapter;
import cn.edu.henu.team.base.BaseActivity;
import cn.edu.henu.team.bean._User;
import cn.edu.henu.team.bean.the_collection;
import cn.edu.henu.team.utils.LocationUtils;

public class FuActivity extends BaseActivity {
    private ImageView back;
    private List<the_collection> data = new ArrayList<>();//必须初始化
    private RecyclerView recyclerView;
    private FuUserAdapter adapter;
    private EditText et_search;
    private Integer fuWei = 5;
    private SmartRefreshLayout refreshSrl;
    private ScrollView scrollView;
    private  _User user = BmobUser.getCurrentUser(_User.class);

    @Override
    protected int initLayout() {
        return R.layout.activity_fu;
    }

    @Override
    protected void initView() {
        back =  findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        refreshSrl = findViewById(R.id.refresh_srl_book);
        refreshSrl.setRefreshHeader(new ClassicsHeader(getApplicationContext()));

        refreshSrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshSrl.finishRefresh(1000/*,false*/);
                initData();
            }
        });

        recyclerView = findViewById(R.id.recyclerview);
        scrollView = findViewById(R.id.scroll);
        if (scrollView != null) {
            scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
                @Override
                public void onScrollChanged() {
                }
            });
        }

        et_search = findViewById(R.id.et_search);
        et_search.addTextChangedListener(new TextWatcher() {
            //每次EditText文本改变之前的时候，会回调这个方法
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            //每次EditText文本改变的时候，会回调这个方法
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            //每次EditText文本改变之后的时候，会回调这个方法
            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().equals(""))
                    fuWei = 5;
                else
                    fuWei = Integer.parseInt(s.toString());

                initData();
            }
        });
    }

    @Override
    protected void initData() {
        data.clear();
        BmobQuery<_User> query = new BmobQuery("_User");
        BmobGeoPoint address = new BmobGeoPoint(user.getAddress().getLongitude(), user.getAddress().getLatitude());
        query.addWhereWithinKilometers("address", address, fuWei);
        query.findObjects(new FindListener<_User>() {
            @Override
            public void done(List<_User> users, BmobException e) {
                if (e == null) {
                    if (users.size() != 0) {
                        for (int i = 0; i < users.size(); i++) {
                            if (users.get(i).getObjectId().toString() != null || users.get(i).getNickName().toString() != null || users.get(i).getHeadPortrait().getFileUrl() != null) {
                                if(!user.getObjectId().equals(users.get(i).getObjectId())) {
                                    the_collection flag = new the_collection(users.get(i).getObjectId(), users.get(i).getNickName(), users.get(i).getHeadPortrait().getFileUrl());
                                    flag.setAddressMsg(users.get(i).getAddressMsg());
                                    Double distance = LocationUtils.getDistance(users.get(i).getAddress().getLongitude(), users.get(i).getAddress().getLatitude(), user.getAddress().getLongitude(), user.getAddress().getLatitude());
                                    if (distance != null) {
                                        flag.setDistance(distance);
                                    }
                                    Log.d("++++++++++++0", users.get(i).getAddressMsg() + "   +++++ " + data.size());
                                    data.add(flag);
                                }
                            }
                        }

                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(FuActivity.this);
                        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
                        recyclerView.setLayoutManager(linearLayoutManager);
                        recyclerView.addItemDecoration(new DividerItemDecoration(FuActivity.this,DividerItemDecoration.HORIZONTAL));
                        adapter = new FuUserAdapter(data);
                        recyclerView.setAdapter(adapter);
                    } else {
                        Toast.makeText(getApplicationContext(), "附近还没人呢", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}